package top.toptimus.dto.mail;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

@NoArgsConstructor
@AllArgsConstructor
@Data
public class MailBean {
    private String recipient;   //  邮件接收人
    private String subject; //  邮件主题
    private String content; //  邮件内容

    private String overviewFileName;    //  附件名称
    private File overviewAttachment;    //  附件内容


    public MailBean(
            String recipient
            , String subject
            , String content
            , MultipartFile multipartFile
    ) throws IOException {
        this.recipient = recipient;
        this.subject = subject;
        this.content = content;

        // 用uuid作为文件名，防止生成的临时文件重复
        final File excelFile = File.createTempFile(
                multipartFile.getName()
                , multipartFile.getOriginalFilename().substring(multipartFile.getOriginalFilename().lastIndexOf(".")) //  后缀
        );
        // MultipartFile to File
        multipartFile.transferTo(excelFile);
        this.overviewFileName = multipartFile.getOriginalFilename();
        this.overviewAttachment = excelFile;
    }
}
